home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / examples / chap05 / MuServer.java < prev    next >
Encoding:
Java Source  |  1996-10-07  |  1.3 KB  |  43 lines

  1. // MuServer.java
  2. // Server Application for multiuser system
  3. import java.net.*;
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7. class MuServer{
  8.    static Vector clients = new Vector();
  9.  
  10.    public static void main(String[] args){
  11.       ServerSocket server_socket = null;
  12.       Socket client_socket = null;
  13.       int id;
  14.  
  15.       // open socket on PORT
  16.       try{
  17.          server_socket = new ServerSocket(MuProtocol.PORT);
  18.       } catch(IOException e){
  19.          System.out.println("Could not create socket on: " + MuProtocol.PORT + ", " + e);
  20.          System.exit(1);
  21.       }
  22.  
  23.       System.out.println("Waiting for client connection...");
  24.  
  25.       while(true){
  26.          // accept client's request
  27.          try{
  28.             client_socket = server_socket.accept();
  29.          }catch(IOException e){
  30.             System.out.println("Accept failed: " + MuProtocol.PORT + ", " + e);
  31.             System.exit(1);
  32.          }
  33.          System.out.println("Connection established: " 
  34.                                      + client_socket.getInetAddress());             
  35.          // create one thread for client request and 
  36.          // store it in 'clients' for client management
  37.          id = clients.size();
  38.          clients.insertElementAt(new MuDispatcher(client_socket, clients, id), id);
  39.          System.out.println(" id=" + id);
  40.       }
  41.    }
  42. }
  43.